{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb3dbd3a",
   "metadata": {},
   "source": [
    "# Alpha‑Factory Finance Demo 📈\n",
    "\n",
    "This notebook launches a **single‑node Alpha‑Factory** container, activates\n",
    "the *BTC/GLD momentum* strategy, then queries the FinanceAgent for **positions**\n",
    "and **P&L**.  It runs **online or offline** – if `OPENAI_API_KEY` is missing the\n",
    "container automatically uses a local Φ‑2 model.\n",
    "\n",
    "> ⚠️ Requires Docker running on this machine."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d96a2ed0",
   "metadata": {},
   "source": [
    "## 1 · Parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "244c4106",
   "metadata": {},
   "outputs": [],
   "source": [
    "IMG = \"ghcr.io/montrealai/alphafactory_pro:cpu-slim-latest\"\n",
    "PORT_API = 8000\n",
    "CONTAINER = \"af_nb_demo\"\n",
    "STRATEGY = \"btc_gld\"  # change to your own\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fc59fef",
   "metadata": {},
   "source": [
    "## 2 · Start container"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc945f66",
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess, time, requests, sys, os, json\n",
    "print(\"⏳ Pulling image (skip if cached)…\")\n",
    "subprocess.run([\"docker\", \"pull\", IMG], check=True)\n",
    "\n",
    "print(\"🚀  Starting Alpha‑Factory …\")\n",
    "subprocess.Popen([\n",
    "    \"docker\", \"run\", \"-d\", \"--rm\", \"--name\", CONTAINER,\n",
    "    \"-p\", f\"{PORT_API}:8000\",\n",
    "    \"-e\", f\"FINANCE_STRATEGY={STRATEGY}\",\n",
    "    IMG\n",
    "])\n",
    "\n",
    "# Wait for /health\n",
    "for _ in range(60):\n",
    "    try:\n",
    "        requests.get(f\"http://localhost:{PORT_API}/health\", timeout=1)\n",
    "        break\n",
    "    except Exception:\n",
    "        time.sleep(1)\n",
    "else:\n",
    "    raise RuntimeError(\"API did not start in 60 s\")\n",
    "\n",
    "print(f\"✅  API ready at http://localhost:{PORT_API}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03a822ab",
   "metadata": {},
   "source": [
    "## 3 · Fetch positions & P&L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca19600f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd, requests, IPython.display as disp\n",
    "base = f\"http://localhost:{PORT_API}/api/finance\"\n",
    "positions = requests.get(base + \"/positions\").json()\n",
    "pnl = requests.get(base + \"/pnl\").json()\n",
    "\n",
    "disp.display(pd.json_normalize(positions).style.set_caption(\"Current Positions\"))\n",
    "disp.display(pd.json_normalize(pnl).style.set_caption(\"P&L (USD)\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41a7bd2c",
   "metadata": {},
   "source": [
    "## 4 · Explore the trace‑graph ✨\n",
    "Open [http://localhost:8088](http://localhost:8088) in your browser to watch\n",
    "the planner emit decisions and tool‑calls in real time."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abf41abb",
   "metadata": {},
   "source": [
    "## 5 · Shutdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "895feff1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess, time\n",
    "subprocess.run([\"docker\", \"stop\", CONTAINER])\n",
    "print(\"🛑 Container stopped.\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
